#!/usr/bin/env python
# coding: utf-8

# ### 1.地理编码
# > 1.[地理编码](https://lbs.amap.com/api/webservice/guide/api/georegeo)
# >> 1.1
# 

# ### 准备工作

# In[3]:


import requests,json
import pandas as pd
key='49e02fe30371ebea8ddbd07b08a34fa9'


# ### 地理编码

# In[14]:


import requests


# In[15]:


geocode_url = "https://restapi.amap.com/v3/geocode/geo?parameters"


# In[16]:


cjs_key = "49e02fe30371ebea8ddbd07b08a34fa9"
address ="广东省广州市中山大学南方学院"
params ={
    "key":cjs_key,
    "address":address,
    "output":"json"
}
response = requests.get(url=geocode_url,params=params)
response


# In[17]:


response.json()


# In[7]:


中大南方_location = response.json()['geocodes'][0]['location']
中大南方_location


# ### 地理编码

# In[8]:


import requests


# In[9]:


geocode_url = "https://restapi.amap.com/v3/geocode/geo?parameters"


# In[10]:


cjs_key = "49e02fe30371ebea8ddbd07b08a34fa9"
address = "广东省广州市中山大学南方学院"
params = {
    "key":cjs_key,
    "address":address,
    "output":"json"
}
response = requests.get(url=geocode_url,params=params)


# In[11]:


response


# ### 2.逆地理编码
# 

# In[20]:


def regeo(key,location,extensions="base",radius=None,poitype=None):
    """高德API逆地理编码获取"""
    url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":key,
        "location":location,
        "extensions":extensions,
        "radius":radius,
        "poitype":poitype
    }
    r = requests.get(url,params)
    result = r.json()
    return results


# In[21]:


url_regeo = "https://restapi.amap.com/v3/geocode/regeo?parameters"
params_regeo ={
    "key":cjs_key,
    "location":中大南方_location,
    "extenxions":"all"
}
r = requests.get(url_regeo,params=params_regeo)
r


# In[25]:


r.json()


# In[3]:


import requests
url='https://restapi.amap.com/v3/geocode/regeo?parameters'
key='49e02fe30371ebea8ddbd07b08a34fa9'
location='113.679287,23.632575'
extensions='all'
params={
    'key':key,
    'location':location,
    'extensions':extensions,
    'output':'json'
}
r=reponse=requests.get(url,params=params)
results=r.json()
results


# In[4]:


# 取附近建筑物
pd.json_normalize(results['regeocode']['pois'])['name']


# ### 路径规划
# > * 步行url:https://restapi.amap.com/v3/direction/walking?parameters
# > * 
# 

# In[37]:


# 准备起始点和终点
start_add = "广东省广州市中山大学南方学院"
stop_add = "广东省广州市从化区河堤公园"


# In[18]:


# 函数 
def geocode(key,address,city=None):
    """高德API地理编码获取"""
    url ="https://restapi.amap.com/v3/geocode/geo?parameters"
    params ={
        "key":key,
        "address":address,
        "city":city
    }
    response = requests.get(url,params=params)
    results = response.json()['geocodes'][0]['location']
    return results


# In[19]:


help(geocode)


# In[40]:


起始点_中大南方 = geocode(cjs_key,start_add)
起始点_中大南方


# In[41]:


目的地_河堤公园 = geocode(cjs_key,stop_add)
目的地_河堤公园


# In[42]:


# 起点坐标
address = "广东省广州市中山大学南方学院"
url_geocode = "https://restapi.amap.com/v3/geocode/geo?parameters"
params_geocode ={
    "key":cjs_key,
    "address":address
}
response = requests.get(url=url_geocode,params=params_geocode)


# In[43]:


# 终点坐标
address = "广东省广州市从化区河堤公园"
url_geocode = "https://restapi.amap.com/v3/geocode/geo?parameters"
params_geocode ={
    "key":cjs_key,
    "address":address
}
response = requests.get(url=url_geocode,params=params_geocode)


# In[9]:


#步行
def walking(key,origin,destination,sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/direction/walking?parameters'
    params={
        'key':key,
        'origin':'113.679287,23.632575',
        'destination':'113.586605,23.548852',
        'sig':sig,
        'output':output,
        'callback':callback,     
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results


# In[10]:


origin='113.678600,23.633329'
destination='113.586605,23.548852'
walking(key,origin,destination,sig=None,output='json',callback=None)


# #### 公交车
# url:https://restapi.amap.com/v3/direction/transit/integrated?parameters
# 
# 2.requests.get
# 必填：key,city,origin,destination

# In[20]:


#准备起点、终点
origin = "广东省广州市中山大学南方学院"
destination = "广东省广州市从化区东风地铁站"


# In[21]:


origin = geocode(cjs_key,"广东省广州市中山大学南方学院")
origin


# In[22]:


destination = geocode(cjs_key,"广东省广州市从化区东风地铁站")
destination


# In[23]:


cjs_key = "49e02fe30371ebea8ddbd07b08a34fa9"


# In[24]:


#公交
def transit(origin,location,city,cityid=None,extensions='base',strategy=0,nightflag=0,date=None,time=None,sig=None,output='json',callback=None):   
    url='https://restapi.amap.com/v3/direction/transit/integrated?parameters'   
    params={
        'key':key,
        'origin':origin,
        'destination':location,
        'city':city,
        'cityid':cityid,
        'extensions':extensions,
        'strategy':strategy,
        'nightflag':nightflag,
        'date':date,
        'time':time,
        'sig':sig,
        'output':output,
        'callback':callback,
    }    
    response=requests.get(url,params)    
    results=response.json()
    return results


# In[25]:


origin='113.678600,23.633329'
location='113.586605,23.548852'
city='广州'
strategy='1'
transit(origin,location,city,cityid=None,extensions='base',strategy=strategy,nightflag=0,
            date=None,time=None,sig=None,output='json',callback=None)


# In[41]:


#公交换乘方法
a=transit(origin,location,city,cityid=None,extensions='base',strategy=0,nightflag=0,
            date=None,time=None,sig=None,output='json',callback=None)['route']['transits']
import pandas as pd
pd.json_normalize(a)


# ### 驾车

# In[42]:


origin =geocode(cjs_key,"广东省广州市中山大学南方学院")
origin


# In[43]:


destination = geocode(cjs_key,"广东省广州市番禺区石楼镇人民路")
destination


# In[44]:


def driving(key,origin,destination,extensions):
    url='https://restapi.amap.com/v3/direction/driving?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':destination,
        'extensions':extensions   
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[45]:


origin='113.679287,23.632575'
location='113.485587,22.972097'
extensions='base'
driving(key,origin,location,extensions)


# ### 骑车

# In[46]:


origin = geocode(cjs_key,"广东省广州市番禺区石楼镇莲花山公园")
origin


# In[47]:


destination = geocode(cjs_key,"广东省广州市番禺区石楼镇石楼酒家")
destination


# In[48]:


def bicycling(key,origin,destination):
    url='https://restapi.amap.com/v4/direction/bicycling?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':destination
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[49]:


origin='113.487934,22.968893'
location='113.486385,22.973177'
bicycling(key,origin,location)


# In[50]:


#汽车路径
a=bicycling(key,origin,location)['data']['paths'][0]['steps']
pd.json_normalize(a)['instruction']


# ### 货车

# In[51]:


def truck(key,origin,location,size):
    url='https://restapi.amap.com/v4/direction/truck?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':location, 
        'size':size
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[52]:


origin='113.487934,22.968893'
location='113.236244,23.128529'
type='0'
distance(origin,location)'
size='1'
truck(key,origin,location,size)


# In[53]:


# 货车路径
a=truck(key,origin,location,size)['data']['route']['paths'][0]['steps']
pd.json_normalize(a)['instruction']


# ### 距离测量

# In[54]:


def distance(origins,destination,type=''):
    url='https://restapi.amap.com/v3/distance?parameters'
    cjs_key='49e02fe30371ebea8ddbd07b08a34fa9'
    params={
        'key':cjs_key,
        'origins':origins,
        'destination':destination, 
        'type':type
      
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[55]:


origins='113.679287,23.632575'
destination='113.236244,23.128529'
type='0'
distance(origin,location)


# ### 行政区域查询
# 

# In[56]:


def district(key,keywords=None,subdistrict='1',page='1',offset='20',extensions='base',filter='',callback=None,output='json'):
    url='https://restapi.amap.com/v3/config/district?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'subdistrict':subdistrict,
        'extensions':extensions,
        'page':page,
        'offset':offset,
        'filter':filter,
        'callback':callback,
        'output':output     
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[57]:


keywords='广东'
subdistrict='1'
district(key,keywords,subdistrict='1',page='1',offset='20',extensions='base',filter='',callback=None,output='json')


# ## POI搜索

# #### 关键字搜索

# In[58]:


def text(keywords,city='',extensions='',citylimit='',types=''):
    url='https://restapi.amap.com/v3/place/text?parameters'
    cjs_key='49e02fe30371ebea8ddbd07b08a34fa9'
    params={
        'key':cjs_key,
        'keywords':keywords,
        'city':city,
        'extensions':extensions,
        'citylimit':citylimit,
        'types':types
      
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[59]:


keywords='美食'
city='广州'
citylimit='true'
text(keywords,city,extensions,citylimit)


# #### 周边搜索

# In[60]:


def around(location,keywords=''):
    url='https://restapi.amap.com/v3/place/around?parameters '
    cjs_key='49e02fe30371ebea8ddbd07b08a34fa9'
    params={
        'key':cjs_key,
        'keywords':keywords,
        'location':location   
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[61]:


keywords='美食'
location='113.679287,23.632575'
around(location,keywords)


# #### 多边形搜索

# In[62]:


a = geocode(cjs_key,"广东省广州市番禺区石楼镇石楼酒家")
a


# In[63]:


b = geocode(cjs_key,"广东省广州市番禺区石楼镇人民医院")
b


# In[64]:


c=geocode(cjs_key,"广东省广州市番禺区石楼镇第二中学")
c


# In[65]:


d = geocode(cjs_key,"广东省广州市番禺区石楼镇大井街")
d


# In[66]:


def poly(key,polygon,keywords=None,types=None,offset='20',page='1',extensions='base',sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/place/polygon?parameters '
    params={
        'key':key,
        'polygon':polygon,
        'keywords':keywords,
        'types':types,
        'offset':offset,
        'page':page,
        'extensions':extensions,
        'sig':sig,
        'output':output,
        'callback':callback     
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[67]:


polygon='113.486385,22.973177|113.489369,22.975687|113.486864,22.979730|113.487309,22.973451|113.486385,22.973177'
poly(key,polygon,keywords=None,types=None,offset='20',page='1',extensions='base',sig=None,output='json',callback=None)


# #### id查询

# In[68]:


def detail(id):
    url='https://restapi.amap.com/v3/place/polygon?parameters '
    key=cjs_key
    params={
        'key':key,
        'id':id
      
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results


# In[69]:


id=''
detail(id)


# ### ip定位

# In[75]:


def ip(key,a):
    url='https://restapi.amap.com/v3/ip?parameters'
    key='18ef7dd6aeefc21dade574262c00f213'
    params={
        'key':key,
        'a':a
    }
    response=requests.get(url,params)
    results=response.json()
    return results


# In[76]:


a='114.247.50.2'
ip(key,a)


# ### 静态地图

# In[77]:


def amap(key,location,zoom,size='400*400',scale='1',markers=None,labels=None,paths=None,traffic='0',sig=None):
    url='https://restapi.amap.com/v3/staticmap?parameters'
    params={
    'key':key,
    'location':location,
    'zoom':zoom,
    'scale':scale,
    'markers':markers,
    'labels':labels,
    'paths':paths,
    'traffic':traffic,
    'sig':sig
    }
    r=requests.get(url,params)
    data=r.url
    return data


# In[78]:


location='113.487309,22.973451'
zoom='17'
amap(key,location,zoom,size='400*400',scale='1',markers=None,labels=None,paths=None,traffic=None,sig=None)


# ### 坐标转换

# In[4]:


def convert(key,locations,coordays='',sig='',output=''):
    url='https://restapi.amap.com/v3/assistant/coordinate/convert?parameters'
    params={
        'key':key,
        "locations":locations,
        'coordays':coordays,
        'sig':sig,
        'output':output
    }
    response=requests.get(url,params)
    r=response.json()
    return r


# In[5]:


locations='113.486385,22.973177|113.489369,22.975687'
convert(key,locations,coordays='',sig='',output='')


# ### 天气查询

# In[6]:


def weather(key,city,extensions='base',output=''):
    url='https://restapi.amap.com/v3/weather/weatherInfo?parameters'
    params={
        'key':key,
       'city':city,
        'extensions':extensions,
        'output':output
    }
    response=requests.get(url,params)
    r=response.json()
    return r


# In[7]:


city='110101'
weather(key,city,extensions='base',output='')


# ### 输入提示

# In[10]:


def input(key,keywords,type=None,location=None,city=None,citylimit='false',datatype=None,sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/assistant/inputtips?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'type':type,
        'location':location,
        'city':city,
        'citylimit':citylimit,
        'datatype':datatype,
        'sig':sig,
        'output':output,
        'callback':callback
    }
    response=requests.get(url,params)
    r=response.json()
    return r


# In[9]:


keywords='广东省广州市中山大学南方学院'
input(key,keywords,type=None,location=None,city=None,citylimit='false',datatype=None,sig=None,output='json',callback=None)


# ### 交通态势

# In[11]:


def traffic(key,rectangle,level='5',extensions='base',sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/traffic/status/rectangle?parameters'
    params={
        'key':key,
        'rectangle':rectangle,
        'level':level,
        'extensions':extensions,
        'sig':sig,
        'output':output,
        'callback':callback,
    }
    response=requests.get(url,params)
    r=response.json()
    return r


# In[12]:


rectangle='113.486385,22.973177;113.489369,22.975687'
traffic(key,rectangle,level='5',extensions='base',sig=None,output='json',callback=None)

